package com.tencent.examples.blueking.mysql.service;

import com.tencent.bk.utils.blueking.tools.StringConverters;
import com.tencent.examples.blueking.extend.QueryParam;
import com.tencent.examples.blueking.mysql.extend.biz.CrudBiz;
import com.tencent.examples.blueking.mysql.model.BkStoreTime;
import com.tencent.examples.blueking.mysql.model.JobStoreTime;
import com.tencent.examples.blueking.mysql.repository.JobStoreTimeRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class JobStoreTimeService extends CrudBiz<JobStoreTimeRepository, JobStoreTime> {
    @Override
    public Specification<JobStoreTime> getSearchCondition(Map<String, String> queryHash) {
        return (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>(); //所有的断言
            predicates.add(criteriaBuilder.equal(root.get("deleteStatus").as(Boolean.class), false));
            Join<BkStoreTime, JobStoreTime> join = root.join("bkStoreTime", JoinType.LEFT);
            Long globalId = StringConverters.ToLong(queryHash.get("globalId"));
            if (globalId != null) {
                predicates.add(criteriaBuilder.equal(join.get("globalId").as(Long.class), globalId));
            }
            Long bkStoreTimeId = StringConverters.ToLong(queryHash.get("bkStoreTimeId"));
            if (bkStoreTimeId != null) {
                predicates.add(criteriaBuilder.equal(join.get("id").as(Long.class), bkStoreTimeId));
            }
            String jobStatus = queryHash.get("jobStatus");
            if (StringUtils.isNotEmpty(jobStatus)) {
                if ("true".equals(jobStatus)) {
                    predicates.add(criteriaBuilder.equal(root.get("jobStatus").as(boolean.class), true));
                } else if ("false".equals(jobStatus)) {
                    predicates.add(criteriaBuilder.equal(root.get("jobStatus").as(boolean.class), false));
                }
            }
            String closeTime = queryHash.get("closeTime");
            if (StringUtils.isNotEmpty(closeTime)) {
                predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("closeTime").as(String.class), closeTime));
            }
            String jobTime_startDate = queryHash.get("jobTime_startDate");
            if (StringUtils.isNotEmpty(jobTime_startDate)) {
                predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("jobTime").as(Date.class), StringConverters.ToDateTime(jobTime_startDate)));
            }
            String jobTime_endDate = queryHash.get("jobTime_endDate");
            if (StringUtils.isNotEmpty(jobTime_endDate)) {
                predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("jobTime").as(Date.class), StringConverters.ToDateTime(jobTime_endDate)));
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        };
    }

    @Override
    public List<JobStoreTime> findList(QueryParam queryParam) {
        return dao.findAll(getSearchCondition(queryParam.getSqlMap()));
    }

    @Override
    public Page<JobStoreTime> findList(Pageable pageable, QueryParam queryParam) {
        return dao.findAll(getSearchCondition(queryParam.getSqlMap()), pageable);
    }

    @Override
    public JobStoreTime findModel(Long id) {
        return dao.findOne(id);
    }

    @Override
    @Transactional(readOnly = false)
    public void add(JobStoreTime model) {
        model.setCreateDate(new Date());
        dao.save(model);
    }

    @Override
    @Transactional(readOnly = false)
    public void update(JobStoreTime model) {
        JobStoreTime existModel = dao.findOne(model.getId());
        BeanUtils.copyProperties(model, existModel);
        existModel.setUpdateDate(new Date());
    }

    @Override
    @Transactional(readOnly = false)
    public void addOrUpdate(JobStoreTime model) {
        if (model.getId() != null && model.getId() > 0) {
            update(model);
        } else {
            add(model);
        }
    }

    @Override
    @Transactional(readOnly = false)
    public void delete(Long id) {
        dao.delete(id);
    }

    @Override
    @Transactional(readOnly = false)
    public void delete_logic(JobStoreTime model) {
        JobStoreTime jobStoreTime = dao.findOne(model.getId());
        jobStoreTime.setDeleteStatus(true);
    }
}
